package net.yunxinyong.cec.controller;

import net.yunxinyong.cec.entity.EcnomicInventory;
import net.yunxinyong.cec.service.PassportService;
import net.yunxinyong.cec.utils.CECResult;
import net.yunxinyong.cec.utils.FTPUtils;
import net.yunxinyong.cec.vo.EnterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 *@describe 企业填报入口
 *@author  ttbf
 *@date  2018/9/12
 */
@RestController
@RequestMapping(value = "passport")
public class PassportController {

    @Autowired
    private PassportService passportService;

    /**
     * 登陆
     * @param
     * @return
     */

    @RequestMapping(value = "/enter",method = RequestMethod.POST)
    public CECResult enter(@RequestBody EnterVo enterVo,HttpServletRequest request,
                           HttpServletResponse response){
        EcnomicInventory ecnomicInventory = passportService.enter(enterVo);
        if (ecnomicInventory == null){
            return CECResult.build(503,"企业名称不正确");
        }
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if ("cecId".equals(cookie.getName())) {
                    cookie.setMaxAge(0);
                    cookie.setValue(null);
                    response.addCookie(cookie);
                    continue;
                }
            }
        }

        Cookie cecIdC = new Cookie("cecId", ecnomicInventory.getId().toString());
        cecIdC.setMaxAge(21600);
        cecIdC.setPath("/");
        //authTokenC.setDomain("3c62d9df.ngrok.io");
        response.addCookie(cecIdC);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

        return CECResult.build(200,"企业信息正确",ecnomicInventory);

    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public CECResult register(@RequestBody EnterVo enterVo,HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        EcnomicInventory ecnomicInventory = passportService.register(enterVo, enterVo.getFileName());

        if (ecnomicInventory ==null){
            return CECResult.build(503,"企业信息注册失败");
        }
        Long enter =ecnomicInventory.getId();
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if ("cecId".equals(cookie.getName())) {
                    cookie.setMaxAge(0);
                    cookie.setValue(null);
                    response.addCookie(cookie);
                    continue;
                }
            }
        }

        Cookie cecIdC = new Cookie("cecId", enter.toString());
        cecIdC.setMaxAge(21600);
        cecIdC.setPath("/");
        //authTokenC.setDomain("3c62d9df.ngrok.io");
        response.addCookie(cecIdC);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

        return CECResult.build(200,"企业信息注册完成",ecnomicInventory);

    }

    @RequestMapping(value = "/file",method = RequestMethod.POST)
    public CECResult register(MultipartFile file,HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        if (file == null){

            return CECResult.build(504,"请选择文件");
        }
        file.getSize();
        String fileName = new Date().getTime() + file.getOriginalFilename();
        boolean b = FTPUtils.uploadFile(fileName, file.getInputStream());


        return CECResult.build(200,"文件上传成功",fileName);

    }
}
